package com.haierp.service.purchase.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.haierp.mapper.TaskReceiptMapper;
import com.haierp.model.purchase.TaskReceipt;
import com.haierp.service.purchase.ITaskReceiptService;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.TaskReceiptQueryVO;
import com.haierp.vo.json.PicModel;
import com.wangzhixuan.commons.utils.HaiJsonUtils;

@Service
public class TaskReceiptServiceImpl extends SuperServiceImpl<TaskReceiptMapper, TaskReceipt> implements ITaskReceiptService {

	@Override
	public JsonPageResult<List<TaskReceipt>> queryTaskReceipt(TaskReceiptQueryVO taskReceiptQueryVO) {
		JsonPageResult<List<TaskReceipt>> taskReceiptResult = new JsonPageResult<>();
		//1、查询总的记录数量
		Integer totalCount =  this.baseMapper.queryTaskReceiptCount(taskReceiptQueryVO);
		
		//2、查询分页记录
		if(totalCount!=null&&totalCount!=0){
			taskReceiptResult.buildPage(totalCount, taskReceiptQueryVO);
			List<TaskReceipt> taskReceipts = this.baseMapper.queryTaskReceipt(taskReceiptQueryVO);
			List<TaskReceipt> newTaskReceipts = new ArrayList<>();
			for (TaskReceipt taskReceipt : taskReceipts) {
				PicModel pm = HaiJsonUtils.toBean(taskReceipt.getSkuPic(), PicModel.class);
    			String imgSrc = pm.getPicList().get(0).getUrl();
				taskReceipt.setSkuPic(imgSrc);
				newTaskReceipts.add(taskReceipt);
			}
			taskReceiptResult.setData(newTaskReceipts);
		}else{
			List<TaskReceipt> newTaskReceipts  = new ArrayList<>();
			taskReceiptResult.setData(newTaskReceipts);
		}
		return taskReceiptResult;
	}

	/*@Autowired
	private TaskReceiptMapper taskReceiptMapper;
	
	@Override
	public void createReceipt(Receipt receipt) {
		
		this.baseMapper.insert(receipt);
		 List<TaskReceipt>  taskReceipts = receipt.getTaskReceiptList();
		 taskReceipts.forEach(
				 taskReceipt->taskReceipt.setReceiptId(receipt.getId())
				 );
		 taskReceiptMapper.insertBatch(taskReceipts);
	}*/


}
